import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MulterModule } from '@nestjs/platform-express';
import * as path from 'path';
import { diskStorage } from 'multer';
import { FileController } from './common/file/file.controller';
import { FileService } from './common/file/file.service';
import { AdminModule } from './module/admin.module';
import { Admin } from './entity/admin.entity';
import { JwtService } from '@nestjs/jwt';
import { PermissionModule } from './module/permission.module';
import { Permission } from './entity/permission.entity';
import { LoggerService } from './common/logger/logger.service';
import { UserModule } from './module/user.module';
import { User } from './entity/user.entity';
import { Product } from './entity/product.entity';
import { ProductModule } from './module/product.module';
import { CategoryModule } from './module/category.module';
import { Category } from './entity/category.entity';
import { GenerateModule } from './module/generate.module';
import { Order } from './entity/order.entity';
import { OrderModule } from './module/order.module';
import { ArticleModule } from './module/article.module';
import { Article } from './entity/article.entity';
import { Mercadoproduct } from './entity/mercadoproduct.entity';
import { Mercadosku } from './entity/mercadosku.entity';
import { MercadoproductModule } from './module/mercadoproduct.module';
import { MercadoskuModule } from './module/mercadosku.module';
import { Box } from './entity/box.entity';
import { BoxModule } from './module/box.module';
import { Warehouse } from './entity/warehouse.entity';
import { Inventoryplan } from './entity/inventoryplan.entity';
import { WarehouseModule } from './module/warehouse.module';
import { InventoryplanModule } from './module/inventoryplan.module';
import { ExcelService } from './common/excel/excel.service';
import { Outboundplan } from './entity/outboundplan.entity';
import { OutboundplanModule } from './module/outbound.module';
import { SitemapService } from './service/sitemap.service';
import { StoreaddressModule } from './module/storeaddress.module';
import { Storeaddress } from './entity/storeaddress.entity';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env', `.env.${process.env.NODE_ENV}`],
    }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: process.env.DB_HOST,
      port: +process.env.DB_PORT,
      username: process.env.DB_USER,
      password: process.env.DB_PASS,
      database: process.env.DB_NAME,
      entities: [
        Admin,
        Permission,
        User,
        Product,
        Category,
        Order,
        Article,
        Mercadoproduct,
        Mercadosku,
        Box,
        Warehouse,
        Inventoryplan,
        Outboundplan,
        Storeaddress,
      ],
      synchronize: true,
      logging: false,
    }),
    // 文件上传
    MulterModule.register({
      storage: diskStorage({
        // 指定文件存储目录
        destination: path.join(__dirname, '../public/uploads'),
        // 通过时间戳来重命名上传的文件名
        filename: (_, file, callback) => {
          return callback(
            null,
            `${new Date().getTime() + path.extname(file.originalname)}`,
          );
        },
      }),
    }),
    AdminModule,
    PermissionModule,
    UserModule,
    ProductModule,
    CategoryModule,
    GenerateModule,
    OrderModule,
    MercadoproductModule,
    MercadoskuModule,
    ArticleModule,
    BoxModule,
    WarehouseModule,
    InventoryplanModule,
    OutboundplanModule,
    StoreaddressModule,
  ],
  controllers: [AppController, FileController],
  providers: [
    AppService,
    FileService,
    JwtService,
    LoggerService,
    ExcelService,
    SitemapService,
  ],
})
export class AppModule {}
